﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Comm.PublicClass
{
    public class ProtocolRelated
    {
        /// <summary>
        /// 保留固定小数位数，不足补0 
        /// </summary>
        /// <param name="data">目标数据</param>
        /// <param name="point">小数位数</param>
        /// <returns>格式化了小数位数的数据值字符串</returns>
        public static string FormatPoint(double data, int point)
        {
            try
            {
                double tmpdata = Math.Round(data, point);
                string ErrorStr = Convert.ToString(tmpdata);
                int p = ErrorStr.IndexOf('.');
                string pointstr = ErrorStr.Substring(p + 1);
                while (pointstr.Length < point)
                    pointstr += "0";
                if (ErrorStr == "0")
                {
                    ErrorStr = "0."
                             + pointstr;
                }
                else
                {
                    ErrorStr = ErrorStr.Substring(0, p)
                             + "."
                             + pointstr;
                }
                if (data >= 0 && ErrorStr.Substring(0, 1) != "+")
                    ErrorStr = "+"
                             + ErrorStr;
                return ErrorStr;
            }
            catch (Exception ex)
            {
                return null;

            }
        }

        /// <summary>
        /// 获取字符串中包含的特定字符串个数
        /// </summary>
        /// <param name="str">目标字符串</param>
        /// <param name="juststr">特定字符</param>
        /// <returns>特定字符个数</returns>
        public static int GetStrNum(string str, char juststr)
        {
            try
            {
                int resultint = 0;
                for (int i = 0; i < str.Length; i++)
                {
                    if (str[i] == juststr)
                    {
                        resultint += 1;
                    }
                }
                return resultint;
            }
            catch (Exception ex)
            {
                return -1;

            }
        }
      

        /// <summary>
        /// 计算BCC异或校验
        /// </summary>
        /// <param name="aorder">命令帧</param>
        /// <returns>1字节的异或校验</returns>
        public static string GetBCC(string aorder)
        {

            try
            {
                byte C1 = 0;
                byte[] tmpbuf = MyConvert.HexStringToByteArray(aorder);
                for (int i = 0; i < tmpbuf.Length; i++)
                {
                    C1 ^= (byte)tmpbuf[i];
                }
                return Convert.ToString(C1, 16).PadLeft(2, '0').ToUpper();
            }
            catch (Exception ex)
            {
                return null;

            }
        }

       
        /// <summary>
        /// 计算校验和
        /// </summary>
        /// <param name="aorder">命令帧</param>
        /// <returns>1字节的校验和</returns>
        public static string Getsum(string aorder)
        {
            try
            {
                byte C1 = 0;
                byte[] tmpbuf = MyConvert.HexStringToByteArray(aorder);
                for (int i = 0; i < tmpbuf.Length; i++)
                {
                    C1 = (byte)(C1 + tmpbuf[i]);
                }
                return Convert.ToString(C1, 16).PadLeft(2, '0').ToUpper();
            }
            catch (Exception ex)
            {
                return null;

            }
        }

        /// <summary>
        /// 计算Modbus16位的CRC校验;
        /// </summary>
        /// <param name="value">命令帧</param>
        /// <returns>2字节CRC校验</returns>
        public static string CRC16(string aorder)　
        {

            try
            {
                int Crc = 0xFFFF;
                string Crc2 = string.Empty;
                string temp = aorder;
                string temp1 = string.Empty;
                int Sdate = 0;
                if (temp.Length % 2 != 0)
                    temp = "0" + temp;
                while (temp.Length > 0)
                {
                    temp1 = temp.Substring(0, 2);
                    Sdate = Convert.ToInt32(temp1, 16);
                    Crc2 = Convert.ToString(Crc, 2).PadLeft(16, '0');
                    Sdate = Sdate ^ Convert.ToInt32(Crc2.Substring(8, 8), 2);
                    Crc = Convert.ToInt32(Crc2.Substring(0, 8) + Convert.ToString(Sdate, 2).PadLeft(8, '0'), 2);
                    for (int i = 1; i <= 8; i++)
                    {
                        string tmp = Convert.ToString(Crc, 2).PadLeft(16, '0');
                        if (tmp[15] == '1')
                        {
                            Crc = Crc >> 1;
                            Crc = Crc ^ 0xA001;
                        }
                        else
                            Crc = Crc >> 1;
                    }
                    temp = temp.Substring(2);
                }

                Crc2 = Convert.ToString(Crc, 2).PadLeft(16, '0');
                Crc2 = Crc2.Substring(8, 8) + Crc2.Substring(0, 8);
                Sdate = Convert.ToInt32(Crc2, 2);
                return Convert.ToString(Sdate, 16).ToUpper().PadLeft(4, '0');
            }
            catch (Exception ex)
            {
                return null;

            }
        }


        /// <summary>
        /// 将数据域加33
        /// </summary>
        /// <param name="adata">数据域</param>
        /// <returns>每字节加33后的数据域</returns>
        public static string Add33(string aData)
        {

            try
            {
                byte[] tmpbuf = MyConvert.HexStringToByteArray(aData);
                for (int i = 0; i < tmpbuf.Length; i++)
                {
                    tmpbuf[i] = (byte)(tmpbuf[i] + 0x33);
                }
                return MyConvert.ByteArrayToHexString(tmpbuf);
            }
            catch (Exception ex)
            {
                return null;

            }

        }

        /// <summary>
        /// 将数据域减33
        /// </summary>
        /// <param name="aData">数据域</param>
        /// <returns>每字节减33后的数据域</returns>
        public static string Del33(string aData)
        {
            try
            {
                byte[] tmpbuf = MyConvert.HexStringToByteArray(aData);
                for (int i = 0; i < tmpbuf.Length; i++)
                {
                    tmpbuf[i] = (byte)(tmpbuf[i] - 0x33);
                }
                return MyConvert.ByteArrayToHexString(tmpbuf);
            }
            catch (Exception ex)
            {
                return null;

            }
        }

        /// <summary>
        /// 将十六进制字符串前后倒置 123456->563412
        /// </summary>
        /// <param name="str">需倒置的字符串</param>
        /// <returns>按字节两两倒置后的字符串</returns>
        public static string ChangeAddress(string str)
        {
            try
            {
                StringBuilder newstr = new StringBuilder("");
                for (int i = str.Length - 1; i > 0; i -= 2)
                {
                    newstr.Append(str.Substring(i - 1, 2));
                }
                return newstr.ToString();
            }
            catch (Exception ex)
            {
                return null;

            }

        }

        /// <summary>
        /// 将字符串所有字符前后倒置 123456->654321
        /// </summary>
        /// <param name="str">需倒置的字符串</param>
        /// <returns>字符串中一位一位倒置后的字符串</returns>
        public static string ChangeAddressByOne(string str)
        {

            try
            {
                StringBuilder newstr = new StringBuilder("");
                for (int i = str.Length - 1; i >= 0; i -= 1)
                {
                    newstr.Append(str.Substring(i, 1));
                }
                return newstr.ToString();
            }
            catch (Exception ex)
            {
                return null;

            }
        }

        /// <summary>
        /// 字符串每隔两个字符加一个空格
        /// </summary>
        /// <param name="yourstr"></param>
        /// <returns></returns>
        public static string AddBlank(string yourstr)
        {
            string resultstr = string.Empty;
            if (yourstr.Length < 3)
            {
                return yourstr;
            }
            for (int i = 0; i < yourstr.Length; i += 2)
            {
                if (i == yourstr.Length - 2)
                    resultstr += yourstr[i].ToString() + yourstr[i + 1].ToString();
                else
                    resultstr += yourstr[i].ToString() + yourstr[i + 1].ToString() + " ";
            }
            return resultstr;
        }

        /// <summary>
        /// 删除空格
        /// </summary>
        /// <param name="yourstr"></param>
        /// <returns></returns>
        public static string DelBlank(string yourstr)
        {
            return yourstr.Replace(" ", "");
        }

    }
}
